package com.aos.iot.socket.common.parser;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DataParserFactor {

    private final Map<String, Map<String, DataParser>> protocolCmdDataParserMap;

    public DataParserFactor(List<DataParser> dataParsers) {
        protocolCmdDataParserMap = new HashMap<>();
        for (DataParser dataParser : dataParsers) {
            String protocol = dataParser.protocol();
            if(!protocolCmdDataParserMap.containsKey(dataParser.protocol())) {
                protocolCmdDataParserMap.put(protocol, new HashMap<>());
            }
            protocolCmdDataParserMap.get(protocol).put(dataParser.cmd(), dataParser);
        }
    }

    public DataParser getDataParser(String protocol, String cmd) {
        return protocolCmdDataParserMap.get(protocol).get(cmd);
    }
}
